﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="TestAddIndexedDenseVector.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Sequential.Tests.DenseVectorTest.DoubleExtensions
{
    #region Usings

    using System;
    using System.Globalization;

    using ExulLibrary.Exceptions;
    using ExulLibrary.Mathematics.LinearAlgebra.Sequential.DoubleExtensions;

    using NUnit.Framework;

    #endregion

    /// <summary>
    ///     Test suite for <see cref="DenseVectorExtensions.Add(DenseVector{double},int[],DenseVector{double})"/>.
    /// </summary>
    [TestFixture]
    [Category("ExulLibrary.Mathematics.LinearAlgebra.Sequential.DoubleExtensions.DenseVectorExtensions")]
    public sealed class TestAddIndexedDenseVector
    {
        /// <summary>
        ///     Tests the method when indexes specify existing elements.
        /// </summary>
        [Test]
        public void WhenAugendIndexesAreValid()
        {
            // Arrange
            var augend = new DenseVector<double>(new double[] { 10, 20, 30 });
            var addend = new DenseVector<double>(new double[] { 1, 2 });
            var augendIndexes = new[] { 0, 2 };

            // Act
            augend.Add(augendIndexes, addend);

            // Assert
            Assert.That(augend.Values, Is.EquivalentTo(new double[] { 11, 20, 32 }));
        }

        /// <summary>
        ///     Tests the method when augendIndexes parameter is empty collection.
        /// </summary>
        [Test]
        public void WhenAugendIndexesCollectionIsEmpty()
        {
            // Arrange
            var augend = new DenseVector<double>(new double[] { 10, 20, 30 });
            var addend = new DenseVector<double>(new double[] { 1, 2 });
            var augendIndexes = new int[0];

            // Act
            augend.Add(augendIndexes, addend);

            // Assert
            Assert.That(augend.Values, Is.EquivalentTo(new double[] { 10, 20, 30 }));
        }

#if SAFE

        /// <summary>
        ///     Tests the method when augend parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAugendIsNull()
        {
            // Arrange
            var addend = new DenseVector<double>(2);
            var augendIndexes = new[] { 0, 2 };

            // Act and Assert
            Assert.That(
                () => DenseVectorExtensions.Add(null, augendIndexes, addend),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("augend"));
        }

        /// <summary>
        ///     Tests the method when augendIndexes parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAugendIndexesIsNull()
        {
            // Arrange
            var augend = new DenseVector<double>(3);
            var addend = new DenseVector<double>(2);

            // Act and Assert
            Assert.That(
                () => augend.Add(null, addend),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("augendIndexes"));
        }

        /// <summary>
        ///     Tests the method when addend parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAddendIsNull()
        {
            // Arrange
            var augend = new DenseVector<double>(3);
            var augendIndexes = new[] { 0, 2 };

            // Act and Assert
            Assert.That(
                () => augend.Add(augendIndexes, null),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("addend"));
        }

        /// <summary>
        ///     Tests the method when an augend index is negative or greater than or equal to the number of elements
        ///     of augend vector.
        /// </summary>
        /// <param name="position">
        ///     The position of index in the storage.
        /// </param>
        /// <param name="augendIndex">
        ///     The index of augend element.
        /// </param>
        [Test]
        public void WhenAnAugendIndexIsOutOfRange([Values(0, 1)] int position, [Values(-1, 3)] int augendIndex)
        {
            // Arrange
            var augend = new DenseVector<double>(3);
            var addend = new DenseVector<double>(2);
            var augendIndexes = new int[2];
            augendIndexes[position] = augendIndex;

            // Act and Assert
            var parameterName = string.Format(CultureInfo.InvariantCulture, "augendIndexes[{0}]", position);
            Assert.That(
                () => augend.Add(augendIndexes, addend),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(parameterName));
        }

#endif
    }
}